package Dao;

import com.ynny.students;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.List;

public class StudentImpl implements StudentDao{

    @Autowired
    JdbcTemplate jdbcTemplate;
    @Override
    public int insert() {
        String sql = "insert into students(number,name,grade,clbum,gender) values(?,?,?,?,?)";
        int update = jdbcTemplate.update(sql, new Object[]{"20030831", "张三", "2023", "Java", "男"});
        return update;
    }

    @Override
    public List<students> selectAll() {
        String sql = "select * from students";
        List<students> students = jdbcTemplate.query(sql, new BeanPropertyRowMapper<students>(students.class));
        return students;
    }

    @Override
    public students getStudents(String number) {
        String sql = "select * from students where number = ?";
        students student = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<students>(students.class), number);
        return student;
    }

    @Override
    public int update(students students) {
        String sql = "update students set name = ?,grade = ?,clbum = ?,gender = ? where number = ?";
        int update = jdbcTemplate.update(sql, students.getName(), students.getGrade(), students.getClbum(), students.getGender(), students.getNumber());
        return update;
    }
}
